﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DownBitCtrl : MonoBehaviour {

    private GameObject waveGroupsGameObj;
    private Groups waveGroups;

    public float downMargin;
    public float downSpeed;

    private bool hasJumpHit = false;
    private bool downing = false;
    private Vector3 orgPos;

    void Awake()
    {
        waveGroupsGameObj = GameObject.Find("GameGroup");
        waveGroups = waveGroupsGameObj.GetComponent<Groups>();
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
        {
            //Debug.Log("DownBitCtrl -> OnCollisionEnter2D");

            if (!hasJumpHit)
            {
                hasJumpHit = true;
                downing = true;
                orgPos = transform.position;

                GetComponent<WoodFloatCtrl>().isFloat = false;
            }
        }
    }

    void FixedUpdate()
    {
        if (hasJumpHit)
        {
            if (downing && transform.position.y > waveGroups.getY(transform.position.x) + 0.3f - downMargin)
            {
                transform.position = new Vector3(transform.position.x, transform.position.y - downSpeed, transform.position.z);
            }
            else if (downing)
            {
                downing = false;
            }
            else if (!downing && transform.position.y < waveGroups.getY(transform.position.x) + 0.3f)
            {
                transform.position = new Vector3(transform.position.x, transform.position.y + downSpeed, transform.position.z);
            }
            else if (transform.position.y >= waveGroups.getY(transform.position.x) + 0.3f)
            {
                transform.position = new Vector3(transform.position.x, waveGroups.getY(transform.position.x) + 0.3f, transform.position.z);
                hasJumpHit = false;
                GetComponent<WoodFloatCtrl>().isFloat = true;
            }
            else
            {
            }
        }
    }
}
